package com.example.handler;


import com.example.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice  // 相当于下面两个同时作用的效果
//@ControllerAdvice
//@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    // 捕获运行时异常
    @ExceptionHandler
    public Result<String> exceptionHandler(RuntimeException ex){
        log.error("异常信息：{}", ex.getMessage());
        // 具体报错业务逻辑
        return Result.error(ex.getMessage());
    }

}
